package com.nowcoder.Offer.BitOperation;

import org.junit.Test;

/**
 * 不使用 + - * / 实现两数之和
 */
public class BitAdd {
    public int Add(int num1 , int num2){
        int n1 = (num1 & num2) << 1;
        int n2 = num1 ^ num2;
        while((n1&n2 )!=0){
            num1 = n1;
            num2 = n2;
            n1 = (num1 & num2) << 1;
            n2 = num1 ^ num2;
        }
        return n1 | n2;
    }

    public int Add2(int n1, int n2){
        while(n2!=0){
            int tmp = n1 ^ n2;
            n2 = (n1 & n2) << 1;
            n1 = tmp;
        }
        return n1;
    }

    @Test
    public void test(){
        System.out.println(Add2(1,2));

        System.out.println((2 ^ 3)<<1);
    }
}
